<?php


/**
 * Description of AdminPresenter
 *
 * @author David Dusek
 */

class ModulePresenter extends AdminPresenter
{
    public function renderDefault() {
        $this->isAllowed("list", TRUE);
        $this->template->title = $this->getTitle("title", "list");
    }
    
    protected function createComponentGrid() {
        $grid = new \Gridito\Grid($this, 'grid');
       
        $qb = $this->em->getRepository('\CMS\Module\Models\Module')->createQueryBuilder('m')->orderBy('m.order');
        $em = $this->em;
        $mr = $this->em->getRepository('\CMS\Module\Models\Module');
        $presenter = $this;
        
        $model = new CMS\DoctrineQueryBuilderModel($qb);
        $model->setPrimaryKey("name");
        $grid->setModel($model);
        
        $grid->addColumn('name', $this->translate('name'));
        $grid->addColumn('active', $this->translate('status'), array(
                'renderer' => function ($module) use ($presenter) {
                    $status = $module->active ? "on" : "off";
                    $status = $module->system ? "system" : $status;
                    $imgSrc = "/CMS/images/status_$status.png";                    
                    $link = \Nette\Utils\Html::el("a")->addAttributes(
                                array(
                                    "class" => "ajax",
                                    "href" => $presenter->link("toggleActive!", $module->name),
                                ));                    
                    $img = \Nette\Utils\Html::el("img")->addAttributes(
                            array(
                                "src" => $imgSrc, 
                                "alt" => $status,
                                "width" => "24px",
                                "height" => "24px"));
                    $link->add($img);
                    echo $link;
                }
            ));        
        $grid->addColumn('version', $this->translate('version'));        
        $grid->addColumn('author', $this->translate('author'));
           
        if ($this->isAllowed("edit")) {
//            $errMsg = $this->translate("moduleDeactiveErr");
//            $grid->addButton('toggleButton', $this->translate('toggleButton'), array(
//                            'icon' => 'ui-icon-bullet',
//                            'handler' => function ($module) use ($grid, $em, $errMsg) {
//                                if ($module->system) {
//                                    $grid->flashMessage($errMsg);
//                                } else {
//                                    $module->active = !$module->active;
//                                    $em->flush();
//                                }
//                            },
//                    ))
//                    ->setShowText(false)
//                    ->setAjax(true);

            $grid->addButton('moduleUp', $this->translate('moveUp'), array(
                            'icon' => 'ui-icon-triangle-1-n',
                            'handler' => function ($module) use ($grid, $mr) {
                                $mr->move($module, \CMS\Module\Models\ModuleRepository::$UP);
                            },
                    ))
                    ->setShowText(false)
                    ->setAjax(true);

            $grid->addButton('moduleDown', $this->translate('moveDown'), array(
                            'icon' => 'ui-icon-triangle-1-s',
                            'handler' => function ($module) use ($grid, $mr) {
                                $mr->move($module, \CMS\Module\Models\ModuleRepository::$DOWN);
                            },
                    ))
                    ->setShowText(false)
                    ->setAjax(true);
        }
    }
    
    /**
     * Prepnuti aktivity modulu a s tim spojene odstraneni prislusnych zdroju.
     * @param string $name Nazev modulu.
     */
    public function handleToggleActive($name) {
        if ($this->isAllowed("edit")) {
            $mr = $this->em->getRepository('\CMS\Module\Models\Module');
            $module = $mr->find($name);

            if ($module !== NULL && !$module->system) {            
                if ($module->active) {
                    $module->active = FALSE;
                    $this->core->runHook("hookUserRemoveResourceByModule", $module->name, "UserModule");
                } else {
                    $module->active = TRUE;
                }
                $this->em->flush();
            }

            $this->invalidateControl("gridWrapper");
        }
    }
}
